package com.exam.online.system.service.impl;

import com.exam.online.system.dao.CourseDAO;
import com.exam.online.system.dao.StudentDAO;
import com.exam.online.system.dao.TeacherCourseDAO;
import com.exam.online.system.dao.TeacherDAO;
import com.exam.online.system.entity.Course;
import com.exam.online.system.entity.Student;
import com.exam.online.system.entity.Teacher;
import com.exam.online.system.entity.TeacherCourse;
import com.exam.online.system.model.TeacherAndCourseModel;
import com.exam.online.system.service.CourseService;
import com.exam.online.system.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yuzhen
 * @date 2019/5/8
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private StudentDAO studentDAO;
    @Autowired
    private CourseDAO courseDAO;
    @Autowired
    private TeacherCourseDAO teacherCourseDAO;
    @Autowired
    private TeacherDAO teacherDAO;

    @Override
    public PageModel findCourseList(Integer stuId, Integer pageNo, Integer pageSize) {
        Student student = studentDAO.selectByPrimaryKey(stuId);
        Integer grade = student.getGrade();
        List<Course> courses = courseDAO.selectByStuIdAndGradeId(stuId, grade, (pageNo - 1) * pageSize, pageSize);
        int total = courseDAO.totalByStuIdAndGradeId(stuId, grade);
        PageModel<Course> pageModel = new PageModel<>();
        pageModel.setPageNo(pageNo);
        pageModel.setPageSize(pageSize);
        pageModel.setDatas(courses);
        pageModel.setTotalPages(total / pageSize + 1);
        pageModel.setTotalRecords(total);
        return pageModel;
    }

    @Override
    public Integer addCourse(String courseName, Integer grade) {
        Course course = new Course();
        course.setCourseName(courseName);
        course.setGrade(grade);
        course.setCreateTime(new Date());
        course.setState(1);
        int l1 = courseDAO.insert(course);
        return l1;
    }

    @Override
    public List<TeacherAndCourseModel> findTeacherAndCourses() {
        List<TeacherCourse> teacherCourses = teacherCourseDAO.selectAll();
        List<TeacherAndCourseModel> result = new ArrayList<>();
        for (TeacherCourse teacherCourse : teacherCourses) {
            Integer courseId = teacherCourse.getCourseId();
            Integer teacherId = teacherCourse.getTeacherId();
            Course course = courseDAO.selectByPrimaryKey(courseId);
            Teacher teacher = teacherDAO.selectByPrimaryKey(teacherId);
            if (course == null || teacher == null) {
                continue;
            }
            TeacherAndCourseModel teacherAndCourseModel = new TeacherAndCourseModel();
            teacherAndCourseModel.setCourseName(course.getCourseName());
            teacherAndCourseModel.setGradeId(course.getGrade());
            teacherAndCourseModel.setTeacherName(teacher.getName());
            result.add(teacherAndCourseModel);
        }
        return result;
    }

}
